package com.bxait.toymail.coupon.service.impl;

import com.bxait.com.to.MemberPrice;
import com.bxait.com.to.SkuReductionTo;
import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.toymail.coupon.entity.MemberPriceEntity;
import com.bxait.toymail.coupon.entity.SkuLadderEntity;
import com.bxait.toymail.coupon.service.MemberPriceService;
import com.bxait.toymail.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.coupon.dao.SkuFullReductionDao;
import com.bxait.toymail.coupon.entity.SkuFullReductionEntity;
import com.bxait.toymail.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    @Qualifier("skuLadderService")
    SkuLadderService skuLadderService;

    @Resource
    @Qualifier("memberPriceService")
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param reductionTo
     * @==============History===============<br/>
     * @Description // TODO  保存商品的优惠信息
     * @Date 2023/3/21 2023/3/21
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.to.SkuReductionTo]
     */
    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {

        // 1.商品的阶梯价格sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        // 设置是否有其他打折
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        // 打折的件数是需要大于0的
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }

        // 2.商品的满减信息sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(reductionTo.getSkuId());
        skuFullReductionEntity.setFullPrice(reductionTo.getFullPrice());
        skuFullReductionEntity.setReducePrice(reductionTo.getReducePrice());
        skuFullReductionEntity.setAddOther(reductionTo.getCountStatus());
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            this.save(skuFullReductionEntity);
        }

        // 3.商品的会员信息sms_member_price
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(member -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(member.getId());
            memberPriceEntity.setMemberPrice(member.getPrice());
            memberPriceEntity.setMemberLevelName(member.getName());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());


        memberPriceService.saveBatch(collect);

    }

}